var can1, can2, ctx1, ctx2;
var can_w, can_h;
var lastTime, deltaTime, now;

var bgPic;

var ane;
var fruit;
var mom;
var baby;
var dust;
var dustPic = [];

var bigTail = [];
var bigEye = [];
var bigSwimOrange = [];
var bigSwimBlue = [];

var babyTail = [];
var babyEye = [];
var babyFade = [];
var mouseX;
var mouseY;

var data;

var wave;
var halo;

document.body.onload = game;

function game () {
	init();
	gameloop();
}
function init () {
	//get canvas and its context
	can1 = document.getElementById("canvas1");//fishes,dust,ui,circle
	ctx1 = can1.getContext('2d');
	can2 = document.getElementById("canvas2");//background,ane,fruits
	ctx2 = can2.getContext('2d');
	// can3 = document.getElementById("canvas3");//background,ane,fruits
	// ctx3 = can3.getContext('2d');
	can_w = can1.width;
	can_h = can1.height;
	
	// ctx3.globalAlpha = 0.6 ;
	// ctx3.strokeStyle = "#3b154e";
	// ctx3.lineCap = "round";
	// ctx3.lineWidth = 20;
	// ctx3.shadowColor = "#fff";
	// ctx3.shadowBlur = 3;



	bgPic = new Image();
	bgPic.src = "./src/background.jpg";

	can1.addEventListener( "mousemove", onMouseMove, false );

	lastTime = Date.now();
	deltaTime = 0;

	
	
	for (var i = 7; i >= 0; i--) {
		babyTail[i] = new Image();
		babyTail[i].src = "./src/babyTail"+i+".png";
	};
	for (var i = 1; i >= 0; i--) {
		babyEye[i] = new Image();
		babyEye[i].src = "./src/babyEye"+i+".png";
	};
	for (var i = 19; i >= 0; i--) {
		babyFade[i] = new Image();
		babyFade[i].src = "./src/babyFade"+i+".png";
	};

	for (var i = 7; i >= 0; i--) {
		bigTail[i] = new Image();
		bigTail[i].src = "./src/bigTail"+i+".png";
	};
	for (var i = 1; i >= 0; i--) {
		bigEye[i] = new Image();
		bigEye[i].src = "./src/bigEye"+i+".png";
	};
	for (var i = 7; i >= 0; i--) {
		bigSwimOrange[i] = new Image();
		bigSwimOrange[i].src = "./src/bigSwim"+i+".png";		
		bigSwimBlue[i] = new Image();
		bigSwimBlue[i].src = "./src/bigSwimBlue"+i+".png";
	};
	for (var i = 6; i >= 0; i--) {
		dustPic[i] = new Image();
		dustPic[i].src = "./src/dust"+i+".png";
	};



	ane = new AneObj();
	ane.init();

	dust = new DustObj();
	dust.init();

	fruit = new FruitObj();
	fruit.init();

	mom = new MomObj();
	mom.init();

	baby = new BabyObj();
	baby.init();

	data = new DataObj();
	ctx1.font = "30px Verdana";
	ctx1.textAlign = "center";

	wave = new WaveObj();
	wave.init();

	halo = new HaloObj();
	halo.init();

	mouseX = 0.5 * can_w;
	mouseY = 0.5 * can_h;


}
function gameloop () {
	window.requestAnimFrame( gameloop );
	now = Date.now();
	deltaTime = now - lastTime;
	lastTime = now;
	if( deltaTime > 40 ){
		deltaTime = 40;
	}
	
	ctx1.clearRect( 0, 0, can_w, can_h );


	drawBackground();

	ane.draw();
	dust.draw();

	fruitMonitor();
	fruit.draw();

	mom.draw();
	momFruitsCollision();

	baby.draw();
	momBabyCollision();

	data.draw();

	wave.draw();

	halo.draw();

}
function onMouseMove ( e ) {
	if( !data.gameover ){
		if( e.offsetX || e.layerX){
			mouseX = e.offsetX == undefined? e.layerX : e.offsetX;
			mouseY = e.offsetY == undefined? e.layerY : e.offsetY;
		}	
	}
	
}